//
//  WMEShopViewController.m
//  everyDay_duiTang
//
//  Created by 恩叔 on 16/2/25.
//  Copyright © 2016年 enshu. All rights reserved.
//

#import "WMEShopViewController.h"
#import <Masonry.h>
@interface WMEShopViewController ()<UIWebViewDelegate>
@property (nonatomic, retain)UIWebView *shopWebView;
@property (nonatomic, retain)UIButton *barButton;
@end

@implementation WMEShopViewController

- (void)dealloc
{
    [_shopWebView release];
    [super dealloc];
}

//- (void)viewWillAppear:(BOOL)animated
//{
//    [super viewWillAppear:animated];
//    if ([self.shopWebView canGoBack]) {
//        UIBarButtonItem *barButton = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"back"] style:UIBarButtonItemStylePlain target:self action:@selector(back)];
//        self.navigationItem.leftBarButtonItems = @[barButton];
//    }
//}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationController.navigationBar.translucent = NO;
    self.navigationItem.title = @"商城";
    
    self.barButton = [UIButton buttonWithType:UIButtonTypeSystem];
    _barButton.frame = CGRectMake(20, 10, 30, 20);
    [_barButton setTitle:@"返回" forState:UIControlStateNormal];
    [_barButton addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];
    _barButton.hidden = YES;
    [self.navigationController.navigationBar addSubview:_barButton];
    
    
    [self createWebView];
}
- (void)createWebView
{
    self.shopWebView = [[UIWebView alloc] initWithFrame:self.view.bounds];
    _shopWebView.delegate = self;
    [self.view addSubview:_shopWebView];
    __block __typeof(&*self)bs = self;
    [_shopWebView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(bs.view.mas_left).with.offset(0);
        make.right.equalTo(bs.view.mas_right).with.offset(0);
        make.top.equalTo(bs.view.mas_top).with.offset(0);
        make.bottom.equalTo(bs.view.mas_bottom).with.offset(0);
    }];
    
    [_shopWebView loadRequest:_loadRequest];
//    _shopWebView.userInteractionEnabled 
}

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
    NSLog(@"!!%@", request);
    NSLog(@"~~%lu", navigationType);
//    if (    UIWebViewNavigationTypeLinkClicked == navigationType) {
//    
//        WMEShopViewController *shopViewController = [[WMEShopViewController alloc] init];
//        shopViewController.loadRequest = request;
//        [self.navigationController pushViewController:shopViewController animated:YES];
//        [shopViewController release];
//        return NO;
//    }
    //首页 由3个网络请求拼接而成
    if ([request.URL.absoluteString containsString:@"http://buy.duitang.com/buy/#/entry?from=app&_urlopentype=page&__urlopentype=pageweb"]||[request.URL.absoluteString containsString:@"http://buy.duitang.com/buy/youliao/?from=app&_urlopentype=page&__urlopentype=pageweb"]||[request.URL.absoluteString containsString:@"http://buy.duitang.com/buy/#/entry?from=app&_urlopentype=page&__urlopentype=pageweb"]){
        _barButton.hidden = YES;

    }else{
        _barButton.hidden = NO;
    }
    return YES;
    
}


-(void) back
{
    if ([self.shopWebView canGoBack]) {
        [self.shopWebView goBack];
    }
    else
    {
        [self.navigationController popViewControllerAnimated:YES];
        _barButton.hidden = YES;
        
    }
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
